package com.bytedance.common.utility.a;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Pair;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class b {
    public static final class a {
        public String a;
        public List b;
        public String c;

        public a(String arg2, String arg3) {
            this.b = new ArrayList();
            this.c = arg2;
            this.a = arg3;
        }

        public a() {

            this.b = new ArrayList();
        }

        public final JSONObject a() {
            JSONObject v0_2;
            if (!android.support.design.a.isTextEmpty(this.a) && !this.b.isEmpty() && this.b.size() != 0 && !android.support.design.a.isTextEmpty(this.c)) {
                JSONObject v1 = new JSONObject();
                try {
                    JSONArray v2 = new JSONArray();
                    Iterator v3 = this.b.iterator();
                    while (v3.hasNext()) {
                        String v0_1 = android.support.design.a.a((String) v3.next(), "SHA-256");
                        if (android.support.design.a.isTextEmpty(v0_1)) {
                            continue;
                        }

                        v2.put(v0_1);
                    }

                    v1.put("mobiles", v2);
                } catch (JSONException v0) {
                    v0.printStackTrace();
                }

                v0_2 = v1;
            } else {
                v0_2 = null;
            }

            return v0_2;
        }
    }

    private static Comparator a;
    private static final Pattern b;

    static {
        a = new c();
        b = Pattern.compile("[^+0-9]");
    }

    @SuppressLint("MissingPermission")
    public static String a(TelephonyManager arg4) {
        String v0 = arg4.getLine1Number();
        if (TextUtils.isEmpty(((CharSequence) v0))) {
            v0 = "";
        } else {
            ArrayList v1 = new ArrayList();
            ((List) v1).add(Pair.create(Pattern.compile("^(\\+86)?(1\\d{10})$"), "$2"));
            v0 = !TextUtils.isEmpty(a(v0, ((List) v1))) ? android.support.design.a.a(v0, "SHA-256") : "";
        }
        return v0;
    }

    private static String a(String arg6, List arg7) {
        String v0_1;
        Matcher v1;
        Pair<Pattern, String> v0;
        String v2 = null;
        if (arg7 == null || arg7.size() == 0 || (android.support.design.a.isTextEmpty(arg6))) {
            v0_1 = v2;
        } else {
            String v3 = b.matcher(arg6).replaceAll("");
            Iterator v4 = arg7.iterator();
            do {
                if (v4.hasNext()) {
                    v0 = (Pair<Pattern, String>) v4.next();
                    v1 = v0.first.matcher(((CharSequence) v3));
                    if (!v1.matches()) {
                        continue;
                    }

                    break;
                } else {
                    return v2;
                }
            }
            while (true);
            v0_1 = v1.replaceAll(v0.second);
            return v0_1;
        }

        return v0_1;
    }

    public static List a(Context arg11, List arg12) {
        ArrayList v0_7;
        Iterator v2_2;
        String v2;
        Cursor v1 = null;
        Cursor v7 = null;
        ContentResolver v0_2;
        Cursor v6 = null;
        HashMap v10 = new HashMap();
        try {
            v0_2 = arg11.getContentResolver();
            v7 = v0_2.query(ContactsContract.Contacts.CONTENT_URI, new String[]{"_id", "display_name"}, null, null, null);
            while (v7.moveToNext()) {
                String v1_1 = v7.getString(0);
                v2 = v7.getString(1);
                if (TextUtils.isEmpty(((CharSequence) v1_1))) {
                    continue;
                }

                if (TextUtils.isEmpty(((CharSequence) v2))) {
                    continue;
                }

                ((Map) v10).put(v1_1, new a(v1_1, v2));
            }
            int v1_2 = 2;
            String[] v2_1 = new String[v1_2];
            v2_1[0] = "contact_id";
            v2_1[1] = "data1";
            v1 = v0_2.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, v2_1, null, null, null);
            while (v1.moveToNext()) {
                String v0_3 = v1.getString(0);
                v2 = v1.getString(1);
                if (TextUtils.isEmpty(((CharSequence) v0_3))) {
                    continue;
                }

                if (TextUtils.isEmpty(((CharSequence) v2))) {
                    continue;
                }

                v2 = a(v2, arg12);
                if (TextUtils.isEmpty(((CharSequence) v2))) {
                    continue;
                }

                Object v0_4 = ((Map) v10).get(v0_3);
                if (v0_4 == null) {
                    continue;
                }

                ((a) v0_4).b.add(v2);
            }

            v2_2 = ((Map) v10).entrySet().iterator();
            while (true) {
                if (!v2_2.hasNext()) {
                    v0_7 = new ArrayList(((Map) v10).values());
                    Collections.sort(((List) v0_7), a);
                    if (v7 != null) {
                        v7.close();
                    }
                    return v0_7;
                }

                int v0_6;
                List v0_5 = ((Map.Entry<?, com.bytedance.common.utility.a.b.a>) v2_2.next()).getValue().b;
                if (v0_5 == null) {
                    v0_6 = 1;
                } else if (v0_5.size() != 0) {
                    v0_6 = 0;
                } else {
                    v0_6 = 1;
                }
                if (v0_6 == 0) {

                } else {
                    v2_2.remove();
                }
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        } finally {
            if (v7 != null) {
                try {
                    v7.close();
                } catch (Exception v1_3) {
                    v1_3.printStackTrace();
                }
                try {
                    if (v1 != null) {
                        v1.close();
                    }
                } catch (Exception e) {
                }
            }
        }
        return new ArrayList();
    }

    @SuppressLint("MissingPermission")
    public static String b(TelephonyManager arg4) {
        String v0 = arg4.getLine1Number();
        if (TextUtils.isEmpty(v0)) {
            v0 = "";
        } else {
            ArrayList v1 = new ArrayList();
            ((List) v1).add(Pair.create(Pattern.compile("^(\\+86)?(1\\d{10})$"), "$2"));
            v0 = a(v0, ((List) v1));
        }
        return v0;
    }
}

